#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging

class Logger:
    def __init__(self, log_file, log_format, date_format, name=__name__):
        # 创建一个loggger
        self.__name = name
        self.log_file = log_file
        self.log_format = log_format
        self.date_format = date_format
        self.logger = logging.getLogger(self.__name)
        self.logger.setLevel(logging.DEBUG)
        # 创建一个handler，用于将日志输出到日志文件中
        fh = logging.FileHandler(self.log_file, mode='w', encoding='utf-8')  # 不拆分日志文件，a指追加模式,w为覆盖模式
        fh.setLevel(logging.WARN)

        # 创建一个handler，用于将日志输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter(self.log_format,
                                      datefmt=self.date_format)
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

    @property
    def get_log(self):
        """定义一个函数，回调logger实例"""
        return self.logger


date_format = '%Y-%m-%d  %H:%M:%S %a'
log_format = ('%(asctime)s %(filename)s[line:%(lineno)d]'
              ' %(levelname)s %(message)s')
log_file = "/tmp/download_rpms.log"
log = Logger(log_file, log_format, date_format, __name__).get_log